﻿using MxWeiXinPF.Common;
using MxWeiXinPF.Web.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MxWeiXinPF.Web.weixin.toupiao2
{
    public partial class index : WeiXinPage
    {
        protected string sort;
        protected string where;
        protected int member_sum;
        protected int pagesize = 10;
        protected int page;
        protected int total;
        protected bool ckHdBegin = true;
        protected int aid = 0;
        protected int wid = 0;
        protected string keywords = "";
        protected string nsort = "";
        protected string banner1 = "";
        protected string banner2 = "";
        protected string bannernum = "";
        BLL.wx_tp_members mBll = new BLL.wx_tp_members();

        protected void Page_Load(object sender, EventArgs e)
        {
            wid = MyCommFun.RequestWid();
            aid = MyCommFun.RequestInt("aid");
            keywords = MyCommFun.QueryString("kw");
            nsort = MyCommFun.QueryString("sort");
            string thisUrl = MyCommFun.getWebSite() + "/weixin/toupiao/index.aspx?wid=" + wid + "&aid=" + aid + "&kw=" + keywords + "&sort=" + nsort;
            OAuth2BaseProc(wid, thisUrl);
            jssdkInit(wid, MyCommFun.getWebSite() + "/weixin/toupiao/index.aspx?wid=" + wid + "&aid=" + aid);
            member_sum = mBll.GetRecordCount(" actId=" + aid + "  and  status=1");//总的参数活动人数
            sort = getSort();
            where = getWhere();
            //刷新时让页码归1
            MyCommFun.setCookie("xjz_pagesize_" + aid, "1", 1);
            if (!IsPostBack)
            {
                //判断是否可以投票
                ckHd();
                this.rptList.DataSource = mBll.GetList(pagesize, page, where, sort, out total);
                this.rptList.DataBind();

            }
        }


        /// <summary>
        /// 检查活动是否正在进行中;如果在进行中，则展示banner
        /// </summary>
        void ckHd()
        {
            BLL.wx_tp_action hdBll = new BLL.wx_tp_action();
            //判断活动是否开始
            Model.wx_tp_action hdModel = hdBll.GetModel(aid);
            ckHdBegin = hdBll.actionIsHF(hdModel);

            if (ckHdBegin)
            {
                BLL.wx_albums_photo apBll = new BLL.wx_albums_photo();
                IList<Model.wx_albums_photo> aplist = apBll.GetModelList("aId=" + hdModel.albumsTypeId + " and isHidden=0 order by seq");

                if (aplist != null)
                {
                    bannernum = aplist.Count.ToString();
                    int tmpI = 0;
                    for (int i = 0; i < aplist.Count; i++)
                    {
                        banner1 += " <li><img src=\"" + aplist[i].photoPic + "\"></li>";
                        tmpI = i + 1;
                        if (tmpI == 1)
                        {
                            banner2 += "<li class=\"active\">1</li>";
                        }
                        else
                        {
                            banner2 += "<li>" + tmpI + "</li>";
                        }
                    }
                }

            }
        }


        /// <summary>
        /// 条件查询
        /// </summary>
        /// <returns></returns>
        string getWhere()
        {

            if (keywords != "")
            {
                return "actId=" + aid + " and status=1 and (bianma like '%" + keywords + "%' or name like '%" + keywords + "%')";
            }
            else
            {
                return "actId=" + aid + " and status=1";
            }
        }

        /// <summary>
        /// 排序方式
        /// </summary>
        /// <returns></returns>
        string getSort()
        {

            if (nsort == "new")
                return "createdate desc,id desc";
            else if (nsort == "piao")
                return "piao desc,id desc";
            return "id desc";
        }
    }
}